/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.diego.games.tetris;

import br.diego.games.tetris.interfaces.IBlock;
import br.diego.games.tetris.layer.Tetris;
import br.diego.games.tetris.layer.block.*;
import br.diego.games.tetris.util.LayerUtil;
import java.io.IOException;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;

/**
 * @author diego
 */
public class TetrisGameCanvas extends GameCanvas implements Runnable {

    private boolean interrupted = false;
    private LayerManager lm;
    private final Tetris tetris = new Tetris();
    private final Timer timer = new Timer();

    public TetrisGameCanvas() {
        super(true);
        try {
            this.setFullScreenMode(true);
            this.init();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    private TetrisGameCanvas(boolean suppressKeyEvents) {
        super(suppressKeyEvents);
    }

    public void run() {
        Graphics g = getGraphics();
        while (!tetris.gameOver()) {
            try {
                int keyState = getKeyStates();
                if ((keyState & LEFT_PRESSED) != 0) {
                    tetris.left();
                }
                if ((keyState & RIGHT_PRESSED) != 0) {
                    tetris.rigth();
                }
                if ((keyState & KEY_NUM0) != 0) {
                    tetris.down();
                }
               // System.out.print(tetris);
                LayerUtil.updateLayerManager(lm, tetris, 0, 0);
                tetris.getTiledLayer().paint(g);
                this.repaint(); //chama o método paint do canvas
                this.serviceRepaints();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
    private IBlock[] blocks = {new BlockL(),new BlockS(),new BlockT()};
    private void init() throws IOException {
        this.lm = new LayerManager();

        this.tetris.onEvent(new Tetris.Event() {

            public void onEvent(int code) {
                System.out.println("[+++++++++++++++++++++++++++++++ " + code + " ++++++++++++++++++++++++++++++++]");
                final int i = ((new Date().hashCode()) % blocks.length);
                IBlock ib = blocks[i];
                switch (code) {
                    case Tetris.CLEAR_LINE:
                        break;
                    case Tetris.COLISAO_BLOCK:
                        break;
                    case Tetris.COLISAO_HORIZONTAL:
                        break;
                    case Tetris.COLISAO_VERTICAL:
                        break;
                    case Tetris.ROTATE:
                        break;
                    case Tetris.START:
                        tetris.add(ib);
                        timer.scheduleAtFixedRate(new TimerTask() {
                            public void run() {
                                tetris.down();
                            }
                        }, 0, 500);
                        break;
                }
            }
        });
    }
}